package com.hmeter.hadoop.hbase.gui;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.hadoop.hbase.HBaseDeleteSampler;

public class HBaseDeleteSamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = 522709729259732877L;
	
	private JLabeledTextField zookeeperQuorumTextField = null;
	private JLabeledTextField hbaseTableTextField = null;
	private JLabeledTextField rowKeyTextField = null;
	private JLabeledTextField columnFamiliesTextField = null;
	private JLabeledTextField fullColumnQualifiersTextField = null;
	private JLabeledTextField filterColumnFamiliesTextField = null;
	private JLabeledTextField delimiterTextField = null;
	private JCheckBox deleteColumnQualifiersCheckBox = null;
	private JCheckBox deleteColumnFamiliesCheckBox = null;
	
	public HBaseDeleteSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
            
        zookeeperQuorumTextField = new JLabeledTextField("Zookeeper quorum");
        hbaseTableTextField = new JLabeledTextField("HBase table");
        rowKeyTextField = new JLabeledTextField("Row key");
        fullColumnQualifiersTextField = new JLabeledTextField("Full column qualifiers list");
        columnFamiliesTextField = new JLabeledTextField("Column families list");
        filterColumnFamiliesTextField = new JLabeledTextField("Filter column families");
        delimiterTextField = new JLabeledTextField("Delimiter");
        deleteColumnQualifiersCheckBox = new JCheckBox("Delete column qualifiers list");
        deleteColumnFamiliesCheckBox = new JCheckBox("Delete column families list");
        ButtonGroup options = new ButtonGroup();
        options.add(deleteColumnQualifiersCheckBox);
        options.add(deleteColumnFamiliesCheckBox);

        //register events
        changeTextFieldState(deleteColumnQualifiersCheckBox, fullColumnQualifiersTextField);
        changeTextFieldState(deleteColumnFamiliesCheckBox, columnFamiliesTextField);
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        
        inputPanel.add(zookeeperQuorumTextField);
        inputPanel.add(hbaseTableTextField);
        inputPanel.add(rowKeyTextField);
        inputPanel.add(filterColumnFamiliesTextField);
        inputPanel.add(delimiterTextField);
        
        //delete column qualifiers panel
        VerticalPanel deleteColumnQualifiersPanel = new VerticalPanel();
        deleteColumnQualifiersPanel.setBorder(BorderFactory.createTitledBorder("Delete column qualifiers"));
        deleteColumnQualifiersPanel.add(deleteColumnQualifiersCheckBox);
        deleteColumnQualifiersPanel.add(fullColumnQualifiersTextField);
        
        //delete column families panel
        VerticalPanel deleteColumnFamiliesPanel = new VerticalPanel();
        deleteColumnFamiliesPanel.setBorder(BorderFactory.createTitledBorder("Delete column families"));
        deleteColumnFamiliesPanel.add(deleteColumnFamiliesCheckBox);
        deleteColumnFamiliesPanel.add(columnFamiliesTextField);
        
        panel.add(inputPanel);
        panel.add(deleteColumnQualifiersPanel);
        panel.add(deleteColumnFamiliesPanel);
        
        add(panel, BorderLayout.CENTER);
	}
	
	public void changeTextFieldState(JCheckBox checkBox, final JLabeledTextField textField)
	{
		checkBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					textField.setEnabled(true);
				}
				else if (e.getStateChange() == ItemEvent.DESELECTED)
				{
					textField.setEnabled(false);
				}
			}
		}
		);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		zookeeperQuorumTextField.setText("");
		hbaseTableTextField.setText("");
		rowKeyTextField.setText("");
		fullColumnQualifiersTextField.setText("");
		columnFamiliesTextField.setText("");
		filterColumnFamiliesTextField.setText("");
		delimiterTextField.setText("");
		deleteColumnQualifiersCheckBox.setSelected(true);
		deleteColumnFamiliesCheckBox.setSelected(false);
		fullColumnQualifiersTextField.setEnabled(true);
		columnFamiliesTextField.setEnabled(false);
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "HBase Delete Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		HBaseDeleteSampler hbaseDeleteSampler = new HBaseDeleteSampler();
		modifyTestElement((TestElement) hbaseDeleteSampler);
		return (TestElement) hbaseDeleteSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof HBaseDeleteSampler)
		{
			HBaseDeleteSampler hbaseDeleteSampler = (HBaseDeleteSampler) element;
			hbaseDeleteSampler.setZookeeperQuorum(zookeeperQuorumTextField.getText());
			hbaseDeleteSampler.setHBaseTable(hbaseTableTextField.getText());
			hbaseDeleteSampler.setRowKey(rowKeyTextField.getText());
			hbaseDeleteSampler.setFullColumnQualifiers(fullColumnQualifiersTextField.getText());
			hbaseDeleteSampler.setColumnFamilies(columnFamiliesTextField.getText());
			hbaseDeleteSampler.setFilterColumnFamilies(filterColumnFamiliesTextField.getText());
			hbaseDeleteSampler.setDelimiter(delimiterTextField.getText());
			hbaseDeleteSampler.setDeleteColumnQualifiersBool(deleteColumnQualifiersCheckBox.isSelected());
			hbaseDeleteSampler.setDeleteColumnFamiliesBool(deleteColumnFamiliesCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof HBaseDeleteSampler)
		{
			HBaseDeleteSampler hbaseDeleteSampler = (HBaseDeleteSampler) element;
			zookeeperQuorumTextField.setText(hbaseDeleteSampler.getZookeeperQuorum());
			hbaseTableTextField.setText(hbaseDeleteSampler.getHBaseTable());
			rowKeyTextField.setText(hbaseDeleteSampler.getRowKey());
			fullColumnQualifiersTextField.setText(hbaseDeleteSampler.getFullColumnQualifiers());
			columnFamiliesTextField.setText(hbaseDeleteSampler.getColumnFamilies());
			filterColumnFamiliesTextField.setText(hbaseDeleteSampler.getFilterColumnFamilies());
			delimiterTextField.setText(hbaseDeleteSampler.getDelimiter());
			deleteColumnQualifiersCheckBox.setSelected(hbaseDeleteSampler.isDeleteColumnQualifiersBool());
			deleteColumnFamiliesCheckBox.setSelected(hbaseDeleteSampler.isDeleteColumnFamiliesBool());
		}
	}
}
